package com.atguigu.stgy.admin.service.impl;

import com.atguigu.stgy.admin.mapper.FeeValueMapper;
import com.atguigu.stgy.model.entity.FeeValue;
import com.atguigu.stgy.model.vo.FeeKeyVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
//

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.stgy.model.entity.FeeKey;
import com.atguigu.stgy.admin.mapper.FeeKeyMapper;
import com.atguigu.stgy.admin.service.FeeKeyService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FeeKeyServiceImpl extends ServiceImpl<FeeKeyMapper, FeeKey> implements FeeKeyService{
    @Resource
    FeeValueMapper feeValueMapper;
    @Override
    public List<FeeKeyVO> listFeeKeyVOs() {
        //1、查询所有fee_key
        List<FeeKey> feeKeys = this.list();
        //2、查询所有的fee_value数据： 如果fee_value的 fee_key_id等于 fee_key的id值 那么它就是fee_key的属性值
        List<FeeValue> feeValues = feeValueMapper.selectList(null);
        //3、组装数据
        //将feeKeys每个FeeKey对象转为FeeKeyVO对象
//                //map：可以将集合每个对象转为指定类型返回
//                .map(feeKey -> {
//                    FeeKeyVO feeKeyVO = new FeeKeyVO();
//                    //将数据源对象的属性值 拷贝给目标对象的相同数据(两个对象必须有get set方法)
//                    BeanUtils.copyProperties(feeKey, feeKeyVO);
//                    //FeeKeyVO: 相当于  1  停车费
//                    //再从feeValues中筛选出  1 停车费的 值集合设置给 FeeKeyVO
////                    feeValues.stream()
////                            //filer：根据条件筛选数据  true的数据会保留
////                            .filter(feeValue ->  feeValue.getFeeKeyId().equals(feeKeyVO.getId())
////                                    .co;
//                });
        List<FeeKeyVO> feeKeyVOS = feeKeys.stream().map(feeKey -> {
            FeeKeyVO feeKeyVO = new FeeKeyVO();
            //将数据源对象的属性值 拷贝给目标对象的相同数据(两个对象必须有get set方法)
            BeanUtils.copyProperties(feeKey, feeKeyVO);

            List<FeeValue> values = feeValues.stream().filter(feeValue -> feeValue.getFeeKeyId().equals(feeKeyVO.getId()))
                    .collect(Collectors.toList());
            feeKeyVO.setFeeValueList(values);

            return feeKeyVO;
        }).collect(Collectors.toList());


        return feeKeyVOS;
    }
}
